package com.technofovea.hllib;

import com.sun.jna.FromNativeContext;
import com.sun.jna.ToNativeContext;
import com.sun.jna.TypeConverter;

/**
 * 
 * @author Darien Hager
 */
class BoolToByteConverter implements TypeConverter {

    static final Byte BYTE_TRUE = 0x01;
    static final Byte BYTE_FALSE = 0x00;

    public Object fromNative(Object arg0, FromNativeContext arg1) {
        long v = ((Number) arg0).longValue();
        if (v == BYTE_FALSE.longValue()) {
            return Boolean.FALSE;
        } else {
            return Boolean.TRUE;
        }
    }

    public Object toNative(Object arg0, ToNativeContext arg1) {
        Boolean b = (Boolean) arg0;
        if (b.equals(Boolean.TRUE)) {
            return BYTE_TRUE;
        } else {
            return BYTE_FALSE;
        }
    }

    public Class nativeType() {
        return Byte.class;
    }
}
